﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for ManagerBUS
/// </summary>
public class ManagerBUS
{
    private ManagerDA manager;

	public ManagerBUS()
	{
		//
		// TODO: Add constructor logic here
		//
        manager = new ManagerDA();
	}
    public bool insertStudent(Student s)
    {
        return manager.insertStudent(s);
    }
    public bool updateStudent(Student s)
    {
        return manager.updateStudent(s);
    }
    public DataSet insertCompetition(Competition c)
    {
        return manager.insertCompetition(c);
    }

    public bool updateCompetition(Competition c)
    {
        return manager.updateCompetition(c);
    }
    public DataSet searchStudentByName(string name)
    {
        DataSet ds = manager.searchStudentByName(name);
        return ds;
    }
    public DataSet searchStudentById(int id)
    {
        DataSet ds = manager.searchStudentById(id);
        return ds;
    }
    public DataSet getAllStudent()
    {
        DataSet ds = manager.getAllStudent();
        if (!(ds.Tables[0].Rows.Count > 0))
        {
            return null;
        }
        return ds;
    }
    /*
     * xoa Competition co comId
     * - tim tat ca Design co comId
     * - voi moi 1 Design co comId, lay duoc 1 designId
     * - xoa Mark co designId
     * - xoa Design co comId
     * - xoa StaffCompetition co comId
     * - xoa Competition co comId
     */ 
    //XONG
    public bool delCompetition(int id)
    {
        Competition c=new Competition();
        c.ComId=id;
        DataTable dt = manager.getAllDesignsInCompetition(c).Tables[0];
        Design d = new Design();
        StaffDA staff = new StaffDA();
        Mark m;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            d.Id =Convert.ToInt32(dt.Rows[i][0].ToString());
            m = staff.getMarkByDesign(d);
            if (!staff.delMark(m))
            {
                return false;
            }
            if (!staff.delDesign(d))
            {
                return false;
            }
        }
        if (!manager.delStaffCompetitionByCom(c))
        {
            return false;
        }
        return manager.delCompetition(id);
    }

    public DataSet getAllCompetition()
    {
        DataSet ds = new DataSet();
        ds = manager.getAllCompetition();
        return ds;
    }

    public DataSet searchCompetition(string name)
    {
        DataSet ds = new DataSet();
        ds = manager.searchCompetition(name);
        if (!(ds.Tables[0].Rows.Count > 0))
        {
            return null;
        }
        return ds;
    }
    public DataSet getStaffOfComp(int id)
    {
        return manager.getStaffOfComp(id);
    }
    public Competition searchCompetitionById(int comId)
    {
        DataTable dt=manager.searchCompetitionById(comId).Tables[0];
        Competition comp = new Competition();
        DataRow dr = dt.Rows[0];
        comp.ComId = Convert.ToInt32(dr["comId"].ToString());
        comp.Name = dr["name"].ToString();
        comp.FirstPrize = dr["firstPrize"].ToString();
        comp.SecondPrize = dr["secondPrize"].ToString();
        comp.ThirdPrize = dr["thirdPrize"].ToString();
        comp.PostDate = dr["postDate"].ToString();
        comp.StartDate = dr["startDate"].ToString();
        comp.EndDate = dr["endDate"].ToString();
        comp.Description = dr["description"].ToString();
        comp.Quotation = dr["quotation"].ToString();
        comp.Manager.Id = Convert.ToInt32(dr["managerId"].ToString());
        comp.Manager.Username = dr["userName"].ToString();
        comp.CompImage = dr["compImage"].ToString();
        Manager m = searchManager(comp.Manager.Fullname);
        comp.Manager = m;
        return comp;
    }

    public bool insertManager(Manager m)
    {
        return manager.insertManager(m);
    }

    public bool updateManager(Manager m)
    {
        return manager.updateManager(m);
    }

    /*
     * xoa Manager co managerId
     * - tim tat ca cac Competition co managerId
     * - voi 1 Competition co managerId, lay duoc 1 comId
     * - xoa Competition co comId
     * - xoa Manager co managerId
     */
    //XONG
    public bool delManager(Manager m)
    {
        DataTable dt = manager.searchCompetitionByManager(m).Tables[0];
        int comId;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            comId = Convert.ToInt32(dt.Rows[i][0].ToString());
            if (!delCompetition(comId))
            {
                return false;
            }
        }
        return manager.delManager(m);
    }

    public DataSet getAllManager()
    {
        DataSet ds = new DataSet();
        ds = manager.getAllManager();
        if (!(ds.Tables[0].Rows.Count > 0))
        {
            return null;
        }
        return ds;
    }

    public Manager searchManager(string id)
    {
        DataSet ds = manager.searchManager(id);
        if (!(ds.Tables[0].Rows.Count > 0))
        {
            return null;
        }
        Manager m = new Manager();
        DataRow dr = ds.Tables[0].Rows[0];
        m.Id = Convert.ToInt32(dr[0].ToString());
        m.Username = dr[1].ToString();
        m.Password = dr[2].ToString();
        m.Fullname = dr[3].ToString();
        m.Address = dr[4].ToString();
        m.Email = dr[5].ToString();
        m.PhoneNumber = dr[6].ToString();
        return m;
    }
    public bool practiceStaffComp(int compId, int staffId)
    {

        return manager.practiceStaffComp(compId,staffId);
    }
    public Manager searchManagerById(int id)
    {
        DataSet ds = manager.searchManagerById(id);
        /*
        if (!(ds.Tables[0].Rows.Count > 0))
        {
            return null;
        }
         */
        Manager m = new Manager();
        DataRow dr = ds.Tables[0].Rows[0];
        m.Id = Convert.ToInt32(dr[0].ToString());
        m.Username = dr[1].ToString();
        m.Password = dr[2].ToString();
        m.Fullname = dr[3].ToString();
        m.Address = dr[4].ToString();
        m.Email = dr[5].ToString();
        m.PhoneNumber = dr[6].ToString();
        return m;
    }

    public bool insertStaff(Staff s)
    {
        return manager.insertStaff(s);
    }

    public bool updateStaff(Staff s)
    {
        return manager.updateStaff(s);
    }

    /*
     * xoa Staff co staffId
     * - xoa StaffCompetition co staffId
     * - tim tat ca Mark co staffId
     * - voi 1 Mark co staffId, lay duoc 1 designId
     * - xoa Design co designId
     * - xoa Mark co staffId
     * - xoa Staff co staffId
     */
    //XONG
    public bool delStaff(Staff s)
    {
        if (!manager.delStaffCompetitionByStaff(s))
        {
            return false;
        }
        StaffDA staff = new StaffDA();
        DataTable dt = staff.getMarkByStaff(s).Tables[0];
        Design d = new Design();
        Mark m;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            d.Id = Convert.ToInt32(dt.Rows[i][2].ToString());
            m = staff.getMarkByDesign(d);
            if (!staff.delMark(m))
            {
                return false;
            }
            if (!staff.delDesign(d))
            {
                return false;
            }
        }
        return manager.delStaff(s);
    }

    public DataSet getAllStaff()
    {
        DataSet ds= manager.getAllStaff(); 
        if (!(ds.Tables[0].Rows.Count > 0))
        {
            return null;
        }
        return ds;
    }

    public DataSet searchStaff(Staff s)
    {
        DataSet ds= manager.searchStaff(s);
        if (!(ds.Tables[0].Rows.Count > 0))
        {
            return null;
        }
        return ds;
    }

    public DataSet searchStaffById(Staff s)
    {
        DataSet ds = manager.searchStaffById(s);
        if (!(ds.Tables[0].Rows.Count > 0))
        {
            return null;
        }
        return ds;
    }
    public DataSet managerLogin(string username, string pass)
    {
        return manager.managerLogin(username,pass);
    }

    public double getMarkDesign(int designId)
    {
        return manager.getMarkDesign(designId);
    }
}